<?php
$path = 'instls/mods/' . basename(dirname(__FILE__));
$myPdo = DbUtils::createPdoInst();
$data = DbUtils::get($myPdo, 'pz2_profile', NULL, NULL, NULL, NULL, NULL, NULL)->d[0];
?>
<style>
.form {
  background-color: #fff;
}
.form td {
  vertical-align: middle;
  padding: 2px 5px;
}
.form th.header {
  text-align: left;
  color: #fff;
  background-color: #e09696;
  padding-left: 5px;
  font-weight: bold;
  height: 25px;
}
.form td.title {
  background-color: #f1d0d0;
  border-top: 1px solid #fff;
  width: 150px;
}
.form td.content {
  width: 200px;
  padding: 0;
}
.form td.content input {
  width: 99%;
  border: solid 1px #ddd;
}
</style>
<table class="form" cellPadding=0 cellSpacing=0>
  <tr><th class="header" colspan=2>Account</th></tr>
  <tr>
    <td class="title">Email</td>
    <td class="content"><input type="text" id="email" value="<?php echo $data->email; ?>"></input></td>
  </tr>
  <tr>
    <td class="title">Password</td>
    <td class="content"><input type="password" id="password" value=""></input></td>
  </tr>
  <tr><th class="header" colspan=2>SMTP Server</th></tr>
  <tr>
    <td class="title">Host</td>
    <td class="content"><input type="text" id="host" value="<?php echo $data->mail_host; ?>"></input></td>
  </tr>
  <tr>
    <td class="title">Account</td>
    <td class="content"><input type="text" id="account" value="<?php echo $data->mail_account; ?>"></input></td>
  </tr>
  <tr>
    <td class="title">Password</td>
    <td class="content"><input type="text" id="mail_password" value="<?php echo $data->mail_password; ?>"></input></td>
  </tr>
  <tr>
    <td class="title">SMTP AUTH</td>
    <td style="text-align: left;"><input type="checkbox" id="smtp_auth" <?php if($data->mail_SMTPAuth == 1) { echo 'checked="true"'; } ?>></input></td>
  </tr>
  <tr>
    <td class="title">Sender's Mail</td>
    <td class="content"><input type="text" id="from_mail" value="<?php echo $data->mail_from_mail; ?>"></input></td>
  </tr>
  <tr>
    <td class="title">Sender's Name</td>
    <td class="content"><input type="text" id="from_name" value="<?php echo $data->mail_from_name; ?>"></input></td>
  </tr>
</table>
<span style="margin-top: 5px;" class="image_button" onclick="check_save();">Save</span>
<script type="text/javascript" src="libs/jquery/jquery-1.6.2.min.js"></script>
<script type="text/javascript" src="incls/js/myutils.js"></script>
<script type="text/javascript" src="incls/js/checkutils.js"></script>
<script type="text/javascript">
  var _checks_add = [];
  var _errors_add = [];
  
  function check_save() {
    CheckUtils.addToArray(_checks_add, $('#email')[0], check_email, _errors_add);
    CheckUtils.addToArray(_checks_add, $('#from_mail')[0], check_email2, _errors_add);
    CheckUtils.addToArray(_checks_add, $('#password')[0], check_password, _errors_add);
    save();
  }

  function save() {
    if (_checks_add.length > 0) {
      setTimeout('save();', 500);
      return;
    }
    if (_errors_add.length > 0) {
      return;
    }
    
    var obj = {
      <?php if (isset($data->id)) { echo 'id:'. $data->id . ','; } ?>
      email: $('#email')[0].value,
      _password: $('#password')[0].value,
      mail_host: $('#host')[0].value,
      mail_SMTPAuth: $('#smtp_auth')[0].checked ? '1' : '0',
      mail_account: $('#account')[0].value,
      mail_password: $('#mail_password')[0].value,
      mail_from_mail: $('#from_mail')[0].value,
      mail_from_name: $('#from_name')[0].value
    };
    $.ajax({
      type: 'POST',
      url: '<?php echo $path . '/' . basename(__FILE__, '.php'); ?>.server.php',
      data: 'action=save&q=' + encodeURIComponent(JSON.stringify(obj)),
      success: function(msg){
        try {
          JSON.parse(msg);
          location.reload();
        } catch (ex) {
          alert( 'Error: ' + ex );
        }
      }
    });
  }

  function check_email(inputElm, callbackFunc, errorArr) {
    if(inputElm.value == '') {
      CheckUtils.addError(inputElm, errorArr, 'Required');
      callbackFunc();
      return;
    }
    var re = /^[a-zA-Z0-9]([a-zA-Z0-9]*[-_.]?[a-zA-Z0-9]+)+@([a-zA-Z0-9]+\.)+[a-zA-Z]{2,}$/;
    if (!re.test(inputElm.value)) {
      CheckUtils.addError(inputElm, errorArr, 'Not a proper email');
    }
    callbackFunc();
  }

  function check_email2(inputElm, callbackFunc, errorArr) {
    var re = /^[a-zA-Z0-9]([a-zA-Z0-9]*[-_.]?[a-zA-Z0-9]+)+@([a-zA-Z0-9]+\.)+[a-zA-Z]{2,}$/;
    if (!re.test(inputElm.value) && inputElm.value != '') {
      CheckUtils.addError(inputElm, errorArr, 'Not a proper Email');
    }
    callbackFunc();
  }

  function check_password(inputElm, callbackFunc, errorArr) {
    if (inputElm.value != '' && inputElm.value.length < 6){
      CheckUtils.addError(inputElm, errorArr, 'At least 6');
    }
    callbackFunc();
  }
</script>